package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.ConfigAppDao;
import com.juyu.miliao.api.dao.mysql.LuckDrawDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.*;
import com.juyu.miliao.api.rabbitMQ.BillDetailBeansMq;
import com.juyu.miliao.api.rabbitMQ.LuckDrawPrizeMq;
import com.juyu.miliao.api.service.cache.PrizeCacheService;
import com.juyu.miliao.common.builder.BillDetailBeansMqMessageBuilder;
import com.juyu.miliao.common.constant.BeansType;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.ConfigApp;
import com.juyu.miliao.common.domain.dto.BillDetailBeansDTO;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.ListGroupUtil;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 卜加超
 * @Date: 2019/7/11 13:44
 * @Description:
 */
@Service
public class PrizeService {

    @Resource
    private PrizeCacheService prizeCacheService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserRedisDao userRedisDao;
    @Resource
    private EnergyLevelService energyLevelService;
    @Resource
    private UserWalletService userWalletService;
    @Resource
    private LuckDrawDao luckDrawDao;
    @Resource
    private AppKeyService appKeyService;
    @Resource
    private BillDetailBeansMq billDetailBeansMq;
    @Resource
    private LuckDrawPrizeMq luckDrawPrizeMq;
    @Resource
    private ConfigAppDao configAppDao;
    @Resource
    private GameDataService gameDataService;

    public List<LuckDrawPrize> getPrizeList() {
        return prizeCacheService.getPrizeList();
    }

    /**
     * 获取用户抽奖次数
     *
     * @param id
     * @return
     */
    public int getLuckDrawNum(long id) {
        int num = 0;
        User user = userInfoService.getUserInfoById((int) id);
        EnergyLevel userEnergyLevel = energyLevelService.getUserEnergyLevel(user.getEnergySum());
        if (user != null) {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(date);
            if (user.getIsSvip() == 2) {
                num = getUserPrizeNum(num, userEnergyLevel, user, time);
            } else if (user.getIsSvip() == 1) {
                num = getUserPrizeNum(num, userEnergyLevel, user, time);
                if (!userRedisDao.isPrizeSVIPNum(user.getUserId(), time)) {
                    num = num + 1;
                } else {
                    if (Integer.parseInt(userRedisDao.getPrizeSVIPNum(user.getUserId(), time)) != 1) {
                        num = num + 1;
                    }
                }
            }
        }
        return num;
    }

    /**
     * 开始抽奖
     *
     * @param id
     * @return
     */
    @Transactional
    public LuckDrawPrize getLuckDrawPrize(long id, HttpServletRequest request) {
        int luckDrawNum = getLuckDrawNum(id);
        if (luckDrawNum <= 0) {
            throw new ConsumeRuntimeException("您的免费次数已经用完");
        }
        //获取每次抽奖寻豆数
         String luckNum = appKeyService.getAppKeyByKey("luckDrawNum").getValue();
        //判断剩余寻豆是否可以抽奖
        UserWallet userWallet = userWalletService.getUserWalletById(id);
        if (userWallet.getBeans() < Integer.parseInt(luckNum)) {
            throw new ConsumeRuntimeException("您的寻豆不足");
        }
        User user = userInfoService.getUserInfoById((int) id);


        List<LuckDrawPrize> luckPrizeList = new ArrayList<>();
        if (user != null) {
           List<LuckDrawPrize> prizeListAll = prizeCacheService.getPrizeListAll();
            luckPrizeList = ListGroupUtil.createRandomList(prizeListAll, 1);
            if (luckPrizeList.size() > 0) {
                //减少抽奖次数
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String time = simpleDateFormat.format(date);
                EnergyLevel userEnergyLevel = energyLevelService.getUserEnergyLevel(user.getEnergySum());
                //减少抽奖次数
                if (user.getIsSvip() == 2) {
                    updateLuckPrizeNum(userEnergyLevel, user, time);
                } else if (user.getIsSvip() == 1) {
                    updateUserSVIPLuckPrizeNum(userEnergyLevel, user, time);
                }

                //动态获取
                int flag = userWalletService.updateBeans(user.getUserId(), Integer.parseInt(luckNum));
                if(flag==1){
                    //保存中奖信息
                    CommonHead com = VerifyUtil.verifyCommonHead(request);
                    ConfigApp configApp = configAppDao.getConfigApp(com.getAppPackageId(), com.getPhoneSystem());
                    LuckDrawLog luckDraw = new LuckDrawLog();
                    luckDraw.setUserId(user.getUserId());
                    luckDraw.setNiceName(user.getNickName());
                    luckDraw.setPrizeName(luckPrizeList.get(0).getPrizeName());
                    luckDraw.setStatus(1);
                    luckDraw.setConsumeBean(Integer.parseInt(luckNum));
                    luckDraw.setPhoneSystem(Integer.parseInt(com.getPhoneSystem()));
                    luckDraw.setChannel(com.getChannel());
                    luckDraw.setGroupType(configApp.getGroupType());
                    luckDraw.setGroupName(configApp.getGroupName());
                    luckDraw.setAmount(luckPrizeList.get(0).getAmount());
                    luckDraw.setPrizeType(luckPrizeList.get(0).getPrizeType());
                    luckDraw.setPrizeUrl(luckPrizeList.get(0).getPrizeUrl());
                    int i = luckDrawDao.insertLuckDraw(luckDraw, user.getUserId());
                    // 添加账单
                    BillDetailBeansMqMessageBuilder builder = new BillDetailBeansMqMessageBuilder();
                    BillDetailBeansDTO billDetailBeansDTO = builder.userId(user.getUserId()).beans(-Integer.parseInt(luckNum))
                            .beansType(BeansType.LUCK_DRAW_CONSUME).
                                    createDate(LocalDate.now(ZoneOffset.of("+8")))
                            .createTime(LocalDateTime.now(ZoneOffset.of("+8"))).build();
                    billDetailBeansMq.getBillDetailBeansMq(billDetailBeansDTO);
                    JSONObject json = new JSONObject();
                    json.put("user",user.getNickName());
                    json.put("action","获得了");
                    json.put("prize",luckPrizeList.get(0).getPrizeName());
                    luckDrawPrizeMq.getLuckDrawPrizeMq(json);
                    gameDataService.increaseAgateNum(LocalDate.now(), id);
                }else{
                    throw new ConsumeRuntimeException("您的寻豆不足");
                }
            }else{
                throw new ConsumeRuntimeException("服务器错误");
            }
        }else{
            throw new ConsumeRuntimeException("用户不存在");
        }
        return luckPrizeList.get(0);
    }


    /**
     * SVIP用户减少抽奖次数
     *
     * @param userEnergyLevel
     * @param user
     * @param time
     */
    public void updateUserSVIPLuckPrizeNum(EnergyLevel userEnergyLevel, User user, String time) {
        if (userEnergyLevel.getEnergyGrade() < 16) {
            if (!userRedisDao.isPrizeNum(user.getUserId(), time)) {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            } else if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) != 1) {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            } else if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                if (!userRedisDao.isPrizeSVIPNum(user.getUserId(), time)) {
                    userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                } else if (Integer.parseInt(userRedisDao.getPrizeSVIPNum(user.getUserId(), time)) != 1) {
                    userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                }else {
                    throw new ConsumeRuntimeException("您的次数已用完");
                }
            } else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }
        }else if (userEnergyLevel.getEnergyGrade()>= 16 &&userEnergyLevel.getEnergyGrade()< 21) {
            if (userRedisDao.isPrizeNum(user.getUserId(), time)) {
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if(userRedisDao.isPrizeSVIPNum(user.getUserId(), time)){
                        if (Integer.parseInt(userRedisDao.getPrizeSVIPNum(user.getUserId(), time)) == 1) {
                            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)) {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                            } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) != 1) {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                            }else {
                                throw new ConsumeRuntimeException("您的次数已用完");
                            }
                        } else {
                            userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                        }
                    }else {
                        userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                    }

                }else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }

            } else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }
        } else if (userEnergyLevel.getEnergyGrade() >= 21 &&userEnergyLevel.getEnergyGrade() < 26) {
            if(userRedisDao.isPrizeNum(user.getUserId(), time)){
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if(userRedisDao.isPrizeSVIPNum(user.getUserId(), time)){
                        if (Integer.parseInt(userRedisDao.getPrizeSVIPNum(user.getUserId(), time)) == 1) {
                            if(userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)){
                                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) == 1) {
                                    if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 21)) {
                                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                    } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 21)) != 1) {
                                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                    }else {
                                        throw new ConsumeRuntimeException("您的次数已用完");
                                    }
                                } else {
                                    userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                                }
                            }else {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                            }
                        } else {
                            userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                        }
                    }else {
                        userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                    }
                } else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }
            }else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }

        }else if (userEnergyLevel.getEnergyGrade()>= 26) {
            if (userRedisDao.isPrizeNum(user.getUserId(), time)) {
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if (userRedisDao.isPrizeSVIPNum(user.getUserId(), time)) {
                        if (Integer.parseInt(userRedisDao.getPrizeSVIPNum(user.getUserId(), time)) == 1) {
                            if (userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)) {
                                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) == 1) {
                                    if (userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 21)) {
                                        if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 21)) == 1) {
                                            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 26)) {
                                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 26, "1");
                                            } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 26)) != 1) {
                                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 26, "1");
                                            }else {
                                                throw new ConsumeRuntimeException("您的次数已用完");
                                            }
                                        } else {
                                            userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                        }
                                    }else {userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                    }
                                }else {
                                    userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                                }

                            }else {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                            }

                        }else {
                            userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                        }
                    }else {
                        userRedisDao.addPrizeSVIPNum(user.getUserId(), time, "1");
                    }
                } else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }
            } else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }
        }
    }

    /**
     * 普通用户减少抽奖次数
     *
     * @param userEnergyLevel
     * @param user
     * @param time
     */
    public void updateLuckPrizeNum(EnergyLevel userEnergyLevel, User user, String time) {
        if(userEnergyLevel.getEnergyGrade() < 16){
            if (!userRedisDao.isPrizeNum(user.getUserId(), time)) {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            } else if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) != 1) {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }else {
                throw new ConsumeRuntimeException("您的次数已用完");
            }
        }else if (userEnergyLevel.getEnergyGrade() >= 16&&userEnergyLevel.getEnergyGrade() <21) {
            if(userRedisDao.isPrizeNum(user.getUserId(), time)){
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)) {
                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                    } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) != 1) {
                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                    }else {
                        throw new ConsumeRuntimeException("您的次数已用完");
                    }
                } else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }
            }else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }
        }else if (userEnergyLevel.getEnergyGrade() >= 21&&userEnergyLevel.getEnergyGrade() <26) {
            if(userRedisDao.isPrizeNum(user.getUserId(), time)){
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if(userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)){
                        if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) == 1) {
                            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 21)) {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                            } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 21)) != 1) {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                            }else {
                                throw new ConsumeRuntimeException("您的次数已用完");
                            }
                        } else {
                            userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                        }
                    }else {
                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                    }
                } else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }
            }else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }

        }else if (userEnergyLevel.getEnergyGrade() >= 26) {
            if(userRedisDao.isPrizeNum(user.getUserId(), time)){
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) == 1) {
                    if(userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)){
                        if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) == 1) {
                            if(userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 21)){
                                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 21)) == 1) {
                                    if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 26)) {
                                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 26, "1");
                                    } else if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 26)) != 1) {
                                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 26, "1");
                                    }else {
                                        throw new ConsumeRuntimeException("您的次数已用完");
                                    }
                                } else {
                                    userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                                }
                            }else {
                                userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 21, "1");
                            }

                        } else {
                            userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                        }
                    }else {
                        userRedisDao.addUserLevelPrizeNum(user.getUserId(), time, 16, "1");
                    }

                } else {
                    userRedisDao.addPrizeNum(user.getUserId(), time, "1");
                }
            }else {
                userRedisDao.addPrizeNum(user.getUserId(), time, "1");
            }

        }
    }

    /**
     * 获取普通用户抽奖次数
     *
     * @param num
     * @param userEnergyLevel
     * @param user
     * @param time
     * @return
     */
    public int getUserPrizeNum(int num, EnergyLevel userEnergyLevel, User user, String time) {

            if (!userRedisDao.isPrizeNum(user.getUserId(), time)) {
                num = 1;
            } else {
                if (Integer.parseInt(userRedisDao.getPrizeNum(user.getUserId(), time)) != 1) {
                    num = 1;
                }
            }
         if (userEnergyLevel.getEnergyGrade() >= 16) {
            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 16)) {
                num = num + 1;
            } else {
                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 16)) != 1) {
                    num = num + 1;
                }
            }
        }
        if (userEnergyLevel.getEnergyGrade() >= 21) {
            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 21)) {
                num = num + 1;
            } else {
                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 21)) != 1) {
                    num = num + 1;
                }
            }
        }
        if (userEnergyLevel.getEnergyGrade() >= 26) {
            if (!userRedisDao.isUserLevelPrizeNum(user.getUserId(), time, 26)) {
                num = num + 1;
            } else {
                if (Integer.parseInt(userRedisDao.getUserLevelPrizeNum(user.getUserId(), time, 26)) != 1) {
                    num = num + 1;
                }
            }
        }
        return num;
    }

    /**
     * 获取中奖列表
     *
     * @return
     */
    public List<LuckDrawLog> getLuckDrawList(long id) {
        List<LuckDrawLog> luckDrawList = luckDrawDao.getLuckDrawList(id);
        if(luckDrawList.size()<50){
            return luckDrawList;
        }
        List<LuckDrawLog> luckDrawLogs = luckDrawList.subList(0, 50);
        return luckDrawLogs;
    }

    /**
     * 获取个人中奖列表
     *
     * @param id
     * @return
     */
    public List<LuckDrawLog> getLuckDrawListByUserId(long id, int type) {

        List<LuckDrawLog> luckDrawList = luckDrawDao.getLuckDrawListByUserId(id);
        if (luckDrawList.size() <= 0) {
            return new ArrayList<>();
        }
        List<LuckDrawLog> list = new ArrayList<>();
        if (type == 0) {
            list.addAll(luckDrawList);
        } else if (type == 1) {
            for (LuckDrawLog luckDraw : luckDrawList) {
                if (luckDraw.getStatus() == 1) {
                    list.add(luckDraw);
                }
            }
        } else if (type == 2) {
            for (LuckDrawLog luckDraw : luckDrawList) {
                if (luckDraw.getStatus() == 2) {
                    list.add(luckDraw);
                }
            }
        } else if (type == 3) {
            for (LuckDrawLog luckDraw : luckDrawList) {
                if (luckDraw.getStatus()>2) {
                    list.add(luckDraw);
                }
            }
        }
        return list;
    }

    public void cancelLuckDraw(long id, int drawId) {
       LuckDrawLog luckDraw = luckDrawDao.getLuckDraw(id,drawId);
       if(luckDraw!=null){
           if(luckDraw.getStatus()==1){
              int flag = luckDrawDao.updateLuckDrawStatus(id,drawId);
              if(flag==1){

                  //获取每次抽奖寻豆数
                  String luckNum = appKeyService.getAppKeyByKey("luckDrawNum").getValue();
                  //返还用户寻豆
                   int i = userWalletService.addBeans(id, Integer.parseInt(luckNum));
                  // 添加账单
                  BillDetailBeansMqMessageBuilder builder = new BillDetailBeansMqMessageBuilder();
                  BillDetailBeansDTO billDetailBeansDTO = builder.userId(id).beans(Integer.parseInt(luckNum))
                          .beansType(BeansType.LUCK_DRAW_INCREASE).
                                  createDate(LocalDate.now(ZoneOffset.of("+8")))
                          .createTime(LocalDateTime.now(ZoneOffset.of("+8"))).build();
                  billDetailBeansMq.getBillDetailBeansMq(billDetailBeansDTO);
              }
           }else if(luckDraw.getStatus()==2){
               throw new ConsumeRuntimeException("奖品已发放");
           }else if(luckDraw.getStatus()==3){
               throw new ConsumeRuntimeException("审核未通过，请查看原因");
           }
       }else {
           throw new ConsumeRuntimeException("您还没有获得");
       }
    }

    public void getSearchForBeans(long userId) {
         UserWallet userWallet = userWalletService.getUserWalletById(userId);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(date);
        if(userRedisDao.isSearchForBeans(userId,time)){
            if(Integer.parseInt(userRedisDao.getSearchForBeans(userId,time))==1){
                throw new ConsumeRuntimeException("你今天已经领过了哦");
            }
        }
         if(userWallet!=null){
             if(userWallet.getBeans()<1000){
                 String beans =appKeyService.getAppKeyByKey("searchForBeans").getValue();

                 if(!userRedisDao.isSearchForBeans(userId,time)){
                     //用户没有领取寻豆
                        int i = userWalletService.addBeans(userId, Integer.parseInt(beans));
                        if(i==1){
                            // 添加账单
                            userRedisDao.addSearchForBeansNum(userId,time,"1");
                            BillDetailBeansMqMessageBuilder builder = new BillDetailBeansMqMessageBuilder();
                            BillDetailBeansDTO billDetailBeansDTO = builder.userId(userId).beans(Integer.parseInt(beans))
                                    .beansType(BeansType.USER_SEARCH_BEANS).
                                            createDate(LocalDate.now(ZoneOffset.of("+8")))
                                    .createTime(LocalDateTime.now(ZoneOffset.of("+8"))).build();
                            billDetailBeansMq.getBillDetailBeansMq(billDetailBeansDTO);
                        }
                 }
             }else {
                 throw new ConsumeRuntimeException("只有当寻豆数量小于1000时，才能领取救济金。");
             }
         }else {
             throw new ConsumeRuntimeException("用户不存在");
         }
    }
}
